import {
  Base
} from "../../models/base.js"
var base = new Base()
Component({
  // externalClasses: ['container'],
  properties: {
    category: Array
  },

  /**
   * 组件的初始数据
   */
  data: {
    inputValue: '',
    hideSearchPannel: true,
    bookList: []

  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 点击 X 图标
    onCancelTap: function(event) {
      this.setData({
        hideSearchPannel: true,
        bookList: []
      })
      this.triggerEvent("showmain", {
        hide: false
      })
    },

    // 删除开头和结尾空格
    trim(str) {
      return str.replace(/(^\s*)|(\s*$)/g, "")
    },

    TrimAll(str)
    {
      var result;
      result = str.replace(/(^\s+)|(\s+$)/g, "");
        result = result.replace(/\s/g, "");
      return result;
    },

    // 失去焦点 去搜索
    onBindBlur: function(event) {
      base.showLoading()
      var key = event.detail.value
      this.setData({
        inputValue: key
      })
      // 去搜索
      if (this.TrimAll(key)) {
        key = this.trim(key)
        console.log(key)
        var params = {
          url: 'search?key=' + key,
          fn: (res) => {
            var bookArr = base.compBuyPriceFromArray(res.data)
            this.setData({
              bookList: bookArr
            })
            wx.hideLoading()
          }
        }
        base.request(params)
      }
    },

    // 获得焦点
    onBindFocus: function() {
      this.setData({
        hideSearchPannel: false
      })
      this.triggerEvent("showmain", {
        hide: true
      })
    },

    onCategoryTap(event) {
      base.showLoading()
      this.setData({
        inputValue: event.currentTarget.dataset.description
      })
      var params = {
        url: "book/by_category/" + event.currentTarget.dataset.id,
        fn: (res) => {
          var bookArr = base.compBuyPriceFromArray(res.data)
          this.setData({
            bookList: bookArr
          })
          wx.hideLoading()
        }
      }
      base.request(params)
    }
  }
})